﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LCUI_Widget_ :MonoBehaviour
{
    //public string Name;
    public SpriteRenderer SpriteRenderer;
    public LCUI_Size_ size;
    public GameObject_ Object_;
    public LCUI_Graph_ Graph_;
    public LCUI_Pos_ pos;
    public int SortingOrder;
    public Vector3 VPos;

    private void Awake()
    {
        SpriteRenderer = gameObject.AddComponent<SpriteRenderer>();
    }

    public void SetGraph(LCUI_Graph_ Graph)
    {
        Graph_ = Graph;
    }

    public void Refresh()
    {
        if(SpriteRenderer !=null)
        {
            if (Graph_ != null)
                SpriteRenderer.sprite = Graph_.sprite;
            SpriteRenderer.sortingOrder = SortingOrder;
        }
    }

    public void SetPos(LCUI_Pos_ pos, bool flipX = true)
    {
        this. pos = pos;
        transform.position = (new Vector3(pos.x, pos.z, -pos.y));
        if(SpriteRenderer.flipX!= flipX)
           SpriteRenderer.flipX = flipX;
        VPos = transform.position;
    }

    public void Move(SpaceObject_ spaceObject, bool flipX=true)
    {
        transform.Translate(new Vector3((float)spaceObject.x_speed * Time.deltaTime*30, (float)spaceObject.z_speed * Time.deltaTime * 60, -(float)spaceObject.y_speed) *Time .deltaTime*0.01f) ;
        VPos = transform.position;
        if (SpriteRenderer.flipX != flipX)
            SpriteRenderer.flipX = flipX;
    }

    public void SetSprite(Sprite sprite)
    {
        SpriteRenderer.sprite = sprite;
        Refresh();
    }
}

public class My_Widget : LCUI
{
    public static LCUI_Widget_ Game_scene;
    public static LCUI_Widget_ Widget_New(string name)
    {
        GameObject @object = new GameObject(name);
        LCUI_Widget_ widget = @object.AddComponent < LCUI_Widget_>();
        //WidgetFuncPairs[FuncType_.FUNC_TYPE_INIT](widget);
        return widget;
    }

    public static void Widget_Resize(LCUI_Widget_ widget, LCUI_Size_ size)
    {
        widget.size = size;
    }

    public static void Widget_SetZIndex(LCUI_Widget_ widget, int value)
    {
        widget.SortingOrder = value;
        widget.Refresh();
    }

    public static void Widget_SetClickableAlpha(LCUI_Widget_ widget,int v1,int v2)
    {
       
    }

    public static void Widget_Container_Add(LCUI_Widget_ widget1, LCUI_Widget_ widget2)
    {
       // widget1 = widget2;
    }

    public static LCUI_Size_ Widget_GetSize(LCUI_Widget_ game_scene)
    {
         Game_scene = game_scene;
        return game_scene.size;
    }

    public static object Widget_GetPrivData(LCUI_Widget_ widget)
    {
        object obj = null;

        if(! KeyValuePairs.TryGetValue(widget,out obj))
        {
            GameObject_ obj_new = new GameObject_();
            KeyValuePairs.Add(widget, obj_new);
            widget.Object_ = obj_new;
            obj = obj_new;
        }
        else
        {
            return KeyValuePairs[widget];
        }
       
        return obj;
    }

    public static int LCUIScreen_GetWidth()
    {
        return Game_scene.size.w;
    }

    public static LCUI_Pos_ Widget_GetPos(LCUI_Widget_ widget)
    {
        return widget.pos;
    }

    public static void Widget_Update(LCUI_Widget_ widget, GameObject_ obj=null)
    {
        if(obj!=null)
        {
            widget.SetPos(obj.space_obj.GetPos(),obj.horiz_flip);
            //widget.Refresh();
           //WidgetFuncPairs[FuncType_.FUNC_TYPE_UPDATE](widget);

        }
        else
        {
            Widget_Update(widget, (GameObject_)Widget_GetPrivData(widget));
        }
    }

    public static void Widget_Draw(LCUI_Widget_ widget, ActionRec_ current=null,int n_frame=1)
    {
        if (current == null)
        {
            GameObject_ obj = (GameObject_)Widget_GetPrivData(widget);
           
            if (obj.current != null)
            {
                if (n_frame < obj.current.action.frame.list.Count)
                {
                    ActionFrameData_ af = ((ActionFrameData_)obj.current.action.frame.list[obj.n_frame]);
                    widget.SetSprite(af.graph.sprite);
                    //WidgetFuncPairs[FuncType_.FUNC_TYPE_DRAW](widget);
                    //Widget_Update(widget, obj);
                }
                else
                {
                    if (obj.current.action.frame.list.Count <= 0) 
                        return;
                    if (n_frame < 1)
                    {
                        n_frame = 1;
                    }
                    ActionFrameData_ af = (ActionFrameData_)(obj.current.action.frame.list[n_frame - 1]);
                    widget.SetSprite(af.graph.sprite);
                }
            }
        }
        else
        {
            if(n_frame<1)
            {
                n_frame = 1;
            }
            ActionFrameData_ af = (ActionFrameData_)(current.action.frame.list[n_frame-1]);
            widget.SetSprite(af.graph.sprite);
           // Widget_Update(widget, (GameObject_)Widget_GetPrivData(widget));
        }
       
    }

    private static void SetSprite(LCUI_Widget_ widget, ActionRec_ current = null, int n_frame=1)
    {
        if (n_frame < 1)
        {
            n_frame = 1;
        }
        ActionFrameData_ af = (ActionFrameData_)(current.action.frame.list[n_frame - 1]);
        widget.SetSprite(af.graph.sprite);
    }
    
    public static void Widget_Destroy(LCUI_Widget_ widget)
    {
        WidgetFuncPairs[FuncType_.FUNC_TYPE_DESTROY](widget);
    }

    public static LCUI_Size_ Graph_GetSize(LCUI_Graph_ graph)
    {
        return new LCUI_Size_(graph.w,graph .h);
    }

    public static void Widget_SetBackgroundImage(LCUI_Widget_ widget, LCUI_Graph_ graph)
    {
        widget.SetGraph ( graph);
    }

    public static void Widget_Show(LCUI_Widget_ widget)
    {
        widget.Refresh();
        //WidgetFuncPairs[FuncType_.FUNC_TYPE_SHOW](widget);
        //if (widget.gameObject == null) return;
        //if(!widget.gameObject.activeSelf)
        //widget.gameObject.SetActive(true);
    }

    public static void Widget_Hide(LCUI_Widget_ widget)
    {
        //WidgetFuncPairs[FuncType_.FUNC_TYPE_HIDE](widget);
        //if (widget.gameObject == null) return;
        //if (widget.gameObject.activeSelf)
        //    widget.gameObject.SetActive(false);
    }

    public static object Widget_NewPrivData(LCUI_Widget_ widget, Type types)
    {
        object obj = new GameObject_();
        KeyValuePairs.Add(widget, obj);
        return obj;
    }

    public static LCUI_Pos_ Pos(int v1,int v2)
    {
        return new LCUI_Pos_(v1, v2);
    }

    public void Widget_SetAlign(LCUI_Widget_ game_scene, ALIGN_TYPE aLIGN_BOTTOM_CENTER, LCUI_Pos_ lCUI_Pos_)
    {
        //game_scene.SetPos(lCUI_Pos_);
    }

    public static int Widget_GetZIndex(LCUI_Widget_ widget)
    {
        return widget.SpriteRenderer.sortingOrder;
    }
    

    public static void Widget_Move(LCUI_Widget_ widget, LCUI_Pos_ pos)
    {
        widget.transform.position = new Vector3(pos.x, pos.y, pos.z);
    }

    public static LCUI_Size_ Size(int w, int h)
    {
        return new LCUI_Size_(w, h);
    }

    public static LCUI_Graph_ Widget_GetSelfGraph(LCUI_Widget_ widget)
    {
        return widget.Graph_;
    }

    public static void WidgetType_Add(string v)
    {
       // WidgetFunc_Add(v, null, FuncType_.No);
    }

    public static Dictionary<FuncType_, Action<LCUI_Widget_>> WidgetFuncPairs = new Dictionary<FuncType_, Action<LCUI_Widget_>>();

    public static void WidgetFunc_Add(string v, Action<LCUI_Widget_> gameObject_ExecFunc = null, FuncType_ fUNC_TYPE_DESTROY = FuncType_.FUNC_TYPE_SHOW)
    {
        WidgetFuncPairs.Add(fUNC_TYPE_DESTROY, gameObject_ExecFunc);
    }

    public static byte Widget_GetAlpha(LCUI_Widget_ widget)
    {
       return  (byte)widget.SpriteRenderer.color.a;
    }

    public static void Widget_SetAlpha(LCUI_Widget_ widget,byte a)
    {
       widget.SpriteRenderer.color=new Color (widget.SpriteRenderer.color.r, widget.SpriteRenderer.color.g, widget.SpriteRenderer.color.b,a);
    }
}

/*----------------- 对齐方式 -------------------*/
public enum ALIGN_TYPE
{
    ALIGN_NONE,     /* 无 */
    ALIGN_TOP_LEFT,     /* 向左上角对齐 */
    ALIGN_TOP_CENTER,   /* 向上中间对齐 */
    ALIGN_TOP_RIGHT,    /* 向右上角对齐 */
    ALIGN_MIDDLE_LEFT,  /* 向中央偏左对齐 */
    ALIGN_MIDDLE_CENTER,    /* 向正中央对齐 */
    ALIGN_MIDDLE_RIGHT, /* 向中央偏由对齐 */
    ALIGN_BOTTOM_LEFT,  /* 向底部偏左对齐 */
    ALIGN_BOTTOM_CENTER,    /* 向底部居中对齐 */
    ALIGN_BOTTOM_RIGHT	/* 向底部偏右对齐 */
}
